package cz.data.domain.market.integration.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cz.data.common.core.DataConstant;
import cz.data.domain.market.integration.service.ServiceExecuteService;
import cz.data.domain.market.model.dto.ServiceExecuteDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = {"服务集成-执行接口"})
@RestController
public class ServiceExecuteController {

    @Autowired
    private ServiceExecuteService serviceExecuteService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "服务集成-集成接口v1")
    @PostMapping("/execute")
    public Object execute(@RequestBody ServiceExecuteDto serviceExecuteDto, HttpServletRequest request) {
        return serviceExecuteService.execute(serviceExecuteDto, request);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "服务集成-集成接口v2")
    @PostMapping("/service/call")
    public Object serviceExecute(@RequestHeader(DataConstant.SERVICE_EXT.SERVICE_NO_HEADER) String serviceNo,
                          @RequestParam(required = false) Map<String, Object> requestParams,
                          @RequestBody(required = false) Map<String, Object> requestBody) {
        Map<String, Object> params = new HashMap<>();
        if (MapUtils.isNotEmpty(requestParams)) {
            params.putAll(requestParams);
        }
        if (MapUtils.isNotEmpty(requestBody)) {
            params.putAll(requestBody);
        }
        return serviceExecuteService.execute(serviceNo, params);
    }

}
